package com.utils;

import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

import java.io.File;

public class OpenCvUtils {

    static {
        String file = OpenCvUtils.class.getClassLoader().getResource("opencv/x64/opencv_java2413.dll").getFile();
        System.out.println("加载dll库->" + file);
        System.load(file);
    }

    public static void main(String[] args) {
        // getStardXy();
    }

    public static int[] getStardXy(File file) {
        // 图片地址
        // String inputImagePath = "G:\\weixin_jump\\src\\train_data\\092705.png";
        String outPath = "D:/demo2.png";
        // 读取图片信息
        Mat image = Highgui.imread(file.getAbsolutePath());
        // 将rgb灰化处理
        Imgproc.cvtColor(image, image, Imgproc.COLOR_BGR2GRAY);
        // 平滑处理
        Imgproc.blur(image, image, new Size(2, 2));
        // 轮廓
        // 使用Canndy检测边缘
        double lowThresh = 50;// 双阀值抑制中的低阀值
        double heightThresh = 100;// 双阀值抑制中的高阀值
        Imgproc.Canny(image, image, lowThresh, heightThresh);
        int width = image.width();
        int height = image.height();
        CoordinateBean lastBean = null;
        for (int y = 0; y < height; y++) {
            CoordinateBean bean = new CoordinateBean(y);
            for (int x = 0; x < width; x++) {
                double[] doubles = image.get(y, x);
                if (doubles[0] != 0.0 && y > 500) {
                    if (bean.getStartX() == null)
                        bean.setStartX(x);
                    else
                        bean.setEndX(x);
                }
            }
            if (bean.isNotNull()) {
                if (lastBean != null) {
                    if (bean.getSub() <= lastBean.getSub())
                        break;
                    else
                        lastBean = bean;
                } else
                    lastBean = bean;
            }

        }
        System.out.println(lastBean);
        for (int i = 0; i < width; i++) {
            double[] doubles = {255, 255, 255};
            image.put(lastBean.getY(), i, doubles);
        }
        Highgui.imwrite(outPath, image);
        int[] result = new int[2];
        result[0] = lastBean.getStartX() + lastBean.getSub() / 2;
        result[1] = lastBean.getY();
        return result;
    }


}